/**
 * 原子类
 * 2019年 01月 03日 星期四
 *
 * @author fireway
 */
package concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest implements Runnable {
    private AtomicInteger mI = new AtomicInteger(0);

    @Override
    public void run() {
        while (true) {
            evenIncrement();
        }
    }

    private void evenIncrement() {
        mI.addAndGet(2);
    }

    public int getValue() {
        return mI.get();
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        AtomicIntegerTest ait = new AtomicIntegerTest();
        exec.execute(ait);

        while (true) {
            int value = ait.getValue();
            if (value % 2 != 0) {
                System.out.println(value);
                System.exit(0);
            }
        }
    }
}
